﻿using ChurchDiary.BLL;
using ChurchDiary.Common;
using ChurchDiary.TableEntity;
using ChurchWebsite.Controllers;
using CrystalDecisions.CrystalReports.Engine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ChurchWebsite.Areas.Church.Controllers
{
    public class ChurchMemberController : BaseController
    {
        //
        // GET: /Church/Members/
        #region ChurchMember Application
        public ActionResult Application()
        {
            return View();
        }
        public JsonResult AddApplication(EntityChurchMemberApplications Application)
        {
            return Json(new BLL_Member().InsertChurchMemberApplication(Application), JsonRequestBehavior.AllowGet);
        }
        public JsonResult UpdateApplication(EntityChurchMemberApplications Application)
        {
            return Json(new BLL_Member().UpdateChurchMemberApplication(Application), JsonRequestBehavior.AllowGet);
        }
        public JsonResult DeleteApplication(Guid dataId)
        {
            return Json(new BLL_Member().DeleteChurchMemberApplication(dataId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetApplications(int PageNo, string SearchKey, string Sort, string SortMode, int PerPage)
        {
            return Json(new BLL_Member().GetChurchMemberApplication(PageNo, SearchKey, Sort, SortMode, PerPage), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetApplication(Guid dataId)
        {
            return Json(new BLL_Member().GetChurchMemberApplication(dataId), JsonRequestBehavior.AllowGet);
        }
        
        #endregion ChurchMemberApplication
        #region ChurchMember Family
        public ActionResult Family()
        {
            return View();
        }
        public JsonResult GetChurchMemberFamilies(int PageNo, string SearchKey, string Sort, string SortMode, int PerPage)
        {
            return Json(new BLL_Member().GetChurchMemberFamilies(PageNo, SearchKey, Sort, SortMode, PerPage), JsonRequestBehavior.AllowGet);
        }
        public JsonResult DeleteChurchMemberFamily(Guid dataId)
        {
            return Json(new BLL_Member().DeleteChurchWorkerFamily(dataId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetChurchMemberFamily(Guid dataId)
        {
            return Json(new BLL_Member().GetChurchMemberFamily(dataId), JsonRequestBehavior.AllowGet);
        }
        #endregion
        #region ChurchMember FamilyInfo
        public ActionResult FamilyInfo()
        {
            return View();
        }
        #endregion
        #region ChurchMember FamilyMembers
        public ActionResult _ChurchMemberFamilyMembers()
        {
            return PartialView();
        }
        #endregion
        #region ChurchMember
        public ActionResult ChurchMembers()
        {
            return View();
        }
        public JsonResult GetChurchMembers(Guid FamilyId)
        {
            return Json(new BLL_Member().GetChurchMembers(FamilyId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult AddChurchMember(EntityChurchMembers Member)
        {
            return Json(new BLL_Member().InsertChurchMembers(Member), JsonRequestBehavior.AllowGet);
        }
        public JsonResult UpdateChurchMember(EntityChurchMembers Member)
        {
            return Json(new BLL_Member().UpdateChurchMembers(Member), JsonRequestBehavior.AllowGet);
        }
        public JsonResult DeleteChurchMember(Guid dataId)
        {
            return Json(new BLL_Member().DeleteChurchMembers(dataId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetChurchMember(Guid dataId)
        {
            return Json(new BLL_Member().GetChurchMember(dataId), JsonRequestBehavior.AllowGet);
        }
        public ActionResult PrintMemberShipform()
        {
            return new Rotativa.UrlAsPdf("http://www.Google.com") { FileName = "UrlTest.pdf" };
        }
        #endregion
        #region ChurchMember
        public ActionResult Members()
        {
            return View();
        }
        public JsonResult GetAllChurchMembers(int PageNo, string SearchKey, string Sort, string SortMode, int PerPage)
        {
            return Json(new BLL_Member().GetChurchMembers(PageNo, SearchKey, Sort, SortMode, PerPage), JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        public JsonResult GetAllChurchMembersByChurch()
        {
            return Json(new BLL_Member().GetAllChurchMemberByChurch(), JsonRequestBehavior.AllowGet);
        }
        #endregion
        #region ChurchMember
        public ActionResult Baptism()
        {

            return View();
        }
        #endregion
        #region ChurchMember Jobtitle Assignment
        public ActionResult JobtitleAssignment()
        {
            return View();
        }
        public JsonResult InsertChurchMemberJobtitle(EntityChurchMemberJobtitle Jobtitle)
        {
            return Json(new BLL_Member().InsertChurchMemberJobtitle(Jobtitle), JsonRequestBehavior.AllowGet);
        }
        public JsonResult UpdateChurchMemberJobtitle(EntityChurchMemberJobtitle Jobtitle)
        {
            return Json(new BLL_Member().UpdateChurchMemberJobtitle(Jobtitle), JsonRequestBehavior.AllowGet);
        }
        public JsonResult DeleteChurchMemberJobtitle(Guid dataId)
        {
            return Json(new BLL_Member().DeleteChurchMemberJobtitle(dataId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetChurchMemberJobtitle(Guid dataId)
        {
            return Json(new BLL_Member().GetChurchMemberJobtitle(dataId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetChurchMemberJobtitles(int PageNo, string SearchKey, string Sort, string SortMode, int PerPage)
        {
            return Json(new BLL_Member().GetChurchMemberJobtitle(PageNo, SearchKey, Sort, SortMode, PerPage), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetAllChurchMemberJobtitles()
        {
            return Json(new BLL_Member().GetChurchMemberJobtitle(), JsonRequestBehavior.AllowGet);
        }
        #endregion
        /*
        public JsonResult GetMembers(int PageNo, string SearchKey, string Sort, string SortMode)
        {
            return Json(new BLL_Member().GetMembers(PageNo,SearchKey,Sort,SortMode),JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetMember(Guid dataId)
        {
            return Json(new BLL_Member().GetMember(dataId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult AddMember(EntityMembers Members)
        {
            return Json(new BLL_Member().InsertMember(Members), JsonRequestBehavior.AllowGet);
        }
        public JsonResult UpdateMember(EntityMembers Members)
        {
            return Json(new BLL_Member().UpdateMember(Members), JsonRequestBehavior.AllowGet);
        }
        public JsonResult DeleteMember(Guid dataId)
        {
            return Json(new BLL_Member().DeleteMember(dataId), JsonRequestBehavior.AllowGet);
        }
        public ActionResult Info()
        {
            return View();
        }
        public JsonResult GetFamilyMembers(Guid dataId)
        {
            return Json(new BLL_Member().GetFamilyMembers(dataId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetFamilyMemberDetails(Guid dataId)
        {
            return Json(new BLL_Member().GetFamilyMember(dataId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult AddFamilyMembers(EntityFamilyMembers FamilyMembers)
        {
            return Json(new BLL_Member().InsertFamilyMembers(FamilyMembers), JsonRequestBehavior.AllowGet);
        }
        public JsonResult UpdateFamilyMembers(EntityFamilyMembers FamilyMembers)
        {
            return Json(new BLL_Member().UpdateFamilyMembers(FamilyMembers), JsonRequestBehavior.AllowGet);
        }
        public JsonResult DeleteFamilyMembers(Guid dataId)
        {
            return Json(new BLL_Member().DeleteFamilyMembers(dataId), JsonRequestBehavior.AllowGet);
        }
        public ActionResult ChurchMembers()
        {
            return View();
        }
        public JsonResult GetAllMembers()
        {
            return Json(new BLL_Member().GetFamilyMembersByChurch(Current.ChurchId, ""), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetJobtitleByRole(Guid dataId)
        {
            return Json(new BLL_Church().GetChurchBoardJobtitlebyRole(dataId), JsonRequestBehavior.AllowGet);
        }

        public JsonResult AddJobtitleRole(EntityMemberRoles MemberRoles)
        {
            string Result =new BLL_Member().InsertMemberRoles(MemberRoles);
            if (Result == "ISUS")
            {
                return Json(Result, JsonRequestBehavior.AllowGet);
            }
            else if (Result == "EIX")
            {
                return Json(Result, JsonRequestBehavior.AllowGet);
            }
            else if (Result == "ERR")
            {
                return Json(Result, JsonRequestBehavior.AllowGet);
            }
            else if (Result == "SME")
            {
                return Json(Result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                BLL_EmailTemplate eTemplate = new BLL_EmailTemplate();
                BLL_Organization email = new BLL_Organization();
                CommonFunction.SendMail(Result, eTemplate.GetEmailContent(eTemplate.GetTemplate("LOGIN"), MemberRoles.MemberId));
                return Json("ISUS");
            }
        }
        public JsonResult UpdateJobtitleRole(EntityMemberRoles MemberRoles)
        {
            return Json(new BLL_Member().UpdateMemberRoles(MemberRoles), JsonRequestBehavior.AllowGet);
        }
        public JsonResult DeleteJobtitleRole(Guid dataId)
        {
            return Json(new BLL_Member().DeleteMemberRoles(dataId), JsonRequestBehavior.AllowGet);
        }

        public ActionResult PrintFamilyReports()
        {
            ReportDocument rd = new ReportDocument();
            rd.Load(Path.Combine(Server.MapPath("~/Reports"), "RptFamilyList.rpt"));
            rd.SetDataSource(new BLL_Member().GetAllFamilyInChurchReport());

            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();
            try
            {
                Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                stream.Seek(0, SeekOrigin.Begin);
                return File(stream, "application/pdf");
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public ActionResult PrintFamilyWithMembersReports()
        {
            ReportDocument rd = new ReportDocument();
            rd.Load(Path.Combine(Server.MapPath("~/Reports"), "RptFamilyWithMembers.rpt"));
            rd.SetDataSource(new BLL_Member().GetFamilyMembersReport());

            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();
            try
            {
                Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                stream.Seek(0, SeekOrigin.Begin);
                return File(stream, "application/pdf");
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public ActionResult PrintFamilyDetails(Guid FamilyId)
        {
            ReportDocument rd = new ReportDocument();
            rd.Load(Path.Combine(Server.MapPath("~/Reports"), "RptFamilyDetails.rpt"));
            rd.SetDataSource(new BLL_Member().GetFamilyDetailsReport(FamilyId));

            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();
            try
            {
                Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                stream.Seek(0, SeekOrigin.Begin);
                return File(stream, "application/pdf");
            }
            catch (Exception ex)
            {
                throw;
            }
        }*/
    }
}
